package com.springboot_nanhua4.controllers;


import lombok.extern.java.Log;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
// 添加ResponseResult的导入（同包下可省略，但显式导入更清晰）
import com.springboot_nanhua4.controllers.ResponseResult;

@ControllerAdvice
@Log
public class GlobalExceptionHandler {

    // ... 以下代码未修改 ...
    @ExceptionHandler(RuntimeException.class)
    public ResponseEntity<ResponseResult> handleRuntimeException(RuntimeException ex) {
        ex.printStackTrace();
        return ResponseEntity.status(
                HttpStatus.BAD_REQUEST
        ).body(ResponseResult.error("服务出现运行时错误,有可能是参数类型错误：" + ex.getMessage()));
    }

    // 这个方法一定要放在这个类最后，它表明当异常无法分类。
    @ExceptionHandler(Exception.class)
    public ResponseEntity<ResponseResult> handleAllException(Exception ex) {
        ex.printStackTrace();
        return ResponseEntity.status(
                HttpStatus.INTERNAL_SERVER_ERROR
        ).body(ResponseResult.error("服务出现致命错误：" + ex.getMessage()));
    }
}